<?php
define('IN_TG','article');
define('IN_JS','article');
define('SCRIPT','article');
session_start();
$tgid = intval($_GET['id']);
require('includes/common.inc.php');
//回帖处理
if(isset($_GET['action']))
{
	if($_GET['action']=='rearticle'){
		_check_login($_POST['back_url']);
		_check_code($_POST['code'],$_SESSION['code']);
		include ROOT_PATH.'includes/check.func.php';
		$tg_reid = $_POST['reid'];
		$tg_type = $_POST['type'];
		$tg_title = _check_post_title($_POST['title'],2,40);
		$tg_content = _check_post_content($_POST['content'],10);
		$tg_username = $_SESSION['username'];
		$tg_date = date('Y-m-d H:i:s',time());
		$userinfo=_fetch_array("select tg_article_time from tg_user where tg_username='".$username."'");
		if(time()-$userinfo['tg_article_time']>$_system['re'] )
		{
			$sql = "insert into tg_article(tg_reid,tg_type,tg_title,tg_content,tg_date,tg_username)values($tg_reid,$tg_type,'$tg_title','$tg_content','$tg_date','$tg_username')";
			$res = _query($sql);
			if($res){
				$sql = "update tg_article set tg_commendcount=tg_commendcount+1 where tg_id = $tg_reid";
				_query($sql);
				$sql2="update tg_user set tg_article_time=".time()." where tg_username='{$tg_username}'";
				_query($sql2);
				$url = "article.php?id=$tgid";
				_location('回复成功',$url);
			}else{
				$url = "article.php?id=$tgid";
				_alert_back('数据插入失败');
			}
		}
		else
		{
			_alert_back("请休息一下");
		}
	}
	else if($_GET['action']=='nice')
	{
		$sql = "update tg_article set tg_nice={$_GET['on']} where tg_id = $tgid";
		$res=_query($sql);
		if($res) {
			$url = "article.php?id=$tgid";
			_location('修改成功', $url);
		}
		else{
			$url = "article.php?id=$tgid";
			_alert_back('数据修改失败');
		}
	}
}
require('header.php');
//分页
$_id = 'id='.$tgid.'&';
$sql = "select tg_id from tg_article where tg_id = $tgid limit 1";
$res1 = _fetch_array($sql);
$arr=array();
if(is_array($res1))
{
	$sql = "select tu.tg_id,ta.tg_username,tg_type,tg_nice,tg_title,tg_content,tg_readcount,tg_commendcount,tg_last_modify_date,tg_date,tg_email,tg_url,tg_sex,tg_face,tg_autograph from tg_article ta inner join tg_user tu on ta.tg_username = tu.tg_username where ta.tg_id = $tgid";
	$res = _fetch_array($sql);
	//设置重复刷新
	if(!isset($_COOKIE['read_'.$tgid]))
	{
		_query("update tg_article set tg_readcount=tg_readcount+1 where tg_id = $tgid");
		setcookie('read_'.$tgid,$tgid,time()+3600);
	}
	$sql = "select tu.tg_id,ta.tg_username,tg_type,tg_nice,tg_title,tg_content,tg_readcount,tg_commendcount,tg_last_modify_date,tg_date,tg_email,tg_url,tg_sex,tg_face,tg_autograph from tg_article ta inner join tg_user tu on ta.tg_username = tu.tg_username where tg_reid = $tgid order by tg_date asc";
	$size = $_system['article'];
	_page($sql,$size);
	$sql .= " limit $_pagenum,$size";
	$reres = _query($sql);
	while($row = _fetch_array_list($reres)){
		$arr[] = $row;
	}
}
else
{
	_alert_back("文章不存在");
}
?>
<div id="article">
	<h2>帖子详情</h2>
		<?php if($res['tg_nice']==1):?>
		<img src="images/nice.gif" alt="精华帖" class="nice" />
		<img src="images/hot.gif" alt="热帖" class="hot" />
		<?php endif;?>
		<div id="subject">
		<dl>
			<dd class="user"><?php echo $res['tg_username'].'('.$res['tg_sex'].')'?>[楼主]</dd>
			<dt><img src="<?php echo $res['tg_face']?>" alt="<?php echo $res['tg_username']?>" /></dt>
			<dd class="message"><a href="javascript:;" name="message" title="<?php echo $res['tg_id']?>">发消息</a></dd>
			<dd class="friend"><a href="javascript:;" name="friend" title="<?php echo $res['tg_id']?>">加为好友</a></dd>
			<dd class="guest">写留言</dd>
			<dd class="flower"><a href="javascript:;" name="flower" title="<?php echo $res['tg_id']?>">给他送花</a></dd>
			<dd class="email">邮件：<a href="mailto:<?php echo $res['tg_email']?>"><?php echo $res['tg_email']?></a></dd>
			<dd class="url">网址：<a href="<?php echo $res['tg_url']?>" target="_blank"><?php echo $res['tg_url']?></a></dd>
		</dl>
		<div class="content">
			<div class="user">
				<span>
					<?php if(isset($_SESSION['username'])):?>
						<?php if($res['tg_username']==$_SESSION['username']):?>
							<?php if($res['tg_nice']==1):?>
											[<a href="article.php?action=nice&on=0&id=<?php echo $tgid ?>">取消精华</a>]
							<?php else:?>
											[<a href="article.php?action=nice&on=1&id=<?php echo $tgid ?>">设立精华</a>]
							<?php endif;?>
											[<a href="article_modify.php?id=<?php echo $tgid ?>">修改</a>]
											[<a href="article_delete.php?id=<?php echo $tgid ?>">删除</a>]
						<?php endif;?>
					<?php endif;?>
					1#
				</span><?php echo $res['tg_username']?> | 发表于：<?php echo $res['tg_date']?>			</div>
			<h3>主题：<?php echo $res['tg_title']?> <img src="images/icon<?php echo $res['tg_type']?>.gif" alt="icon" /> <span>[<a href="#ree" name="re" title="回复1楼的星矢">回复</a>]</span></h3>
			<div class="detail">
				<?php echo _ubb($res['tg_content'])?>
				<p class="autograph"><?php echo $res['tg_autograph']?></p>			</div>
			<?php if($res['tg_last_modify_date'] != '0000-00-00 00:00:00'):?>
				<div class="read">
					<p>本贴已由[<?php echo $res['tg_username']?>]于<?php echo $res['tg_last_modify_date']?>修改过！</p>
					阅读量：(<?php echo $res['tg_readcount']?>) 评论量：(<?php echo $res['tg_commendcount']?>)
				</div>
			<?php endif;?>
		</div>
	</div>
		
	
	<p class="line"></p>
	<?php for($i=0;$i<count($arr);$i++):?>
		<div class="re">
		<dl>
			<dd class="user">
				<?php 
					if($i+$_pagenum==0){
						echo $arr[$i]['tg_username'].'(沙发)('.$arr[$i]['tg_sex'].')';
					}else{
						echo $arr[$i]['tg_username'].'('.$arr[$i]['tg_sex'].')';
					}
				?>
					
			</dd>
			<dt><img src="<?php echo $arr[$i]['tg_face']?>" alt="<?php echo $arr[$i]['tg_username']?>" /></dt>
			<dd class="message"><a href="javascript:;" name="message" title="<?php echo $arr[$i]['tg_id']?>">发消息</a></dd>
			<dd class="friend"><a href="javascript:;" name="friend" title="<?php echo $arr[$i]['tg_id']?>">加为好友</a></dd>
			<dd class="guest">写留言</dd>
			<dd class="flower"><a href="javascript:;" name="flower" title="<?php echo $arr[$i]['tg_id']?>">给他送花</a></dd>
			<dd class="email">邮件：<a href="mailto:<?php echo $arr[$i]['tg_email']?>"><?php echo $arr[$i]['tg_email']?></a></dd>
			<dd class="url">网址：<a href="<?php echo $arr[$i]['tg_url']?>" target="_blank"><?php echo $arr[$i]['tg_url']?></a></dd>
		</dl>
		<div class="content">
			<div class="user">
				<span><?php echo $i+2+$_pagenum;?>#</span><?php echo $arr[$i]['tg_username']?> | 发表于：<?php echo $arr[$i]['tg_date']?>			</div>
			<h3>主题：<?php echo $arr[$i]['tg_title']?> <img src="images/icon<?php echo $arr[$i]['tg_type']?>.gif" alt="icon" /> <span>[<a href="#ree" name="re" title="回复<?php echo $i+2+$_pagenum;?>楼的<?php echo $arr[$i]['tg_username']?>">回复</a>]</span></h3>
			<div class="detail">
				<?php echo _ubb($arr[$i]['tg_content'])?>			
			<p class="autograph"><?php echo _ubb($arr[$i]['tg_autograph'])?></p>			</div>
		</div>
	</div>
	<p class="line"></p>
	<?php endfor;?>
	<div id="page_num"><?php _paging(1);?></div>	
		<a name="ree"></a>
	<form method="post" action="?action=rearticle&id=<?php echo $tgid;?>">
		<input type="hidden" name="reid" value="<?php echo $tgid?>" />
		<input type="hidden" name="type" value="<?php echo $res['tg_type']?>" />
		<input type="hidden" name="back_url" value="<?php echo $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];?>"/>
		<dl>
			<dd>标　　题：<input type="text" name="title" class="text" value="RE:<?php echo $res['tg_title']?>" /> (*必填，2-40位)</dd>
			<dd id="q">贴　　图：　<a href="javascript:;">Q图系列[1]</a>　 <a href="javascript:;">Q图系列[2]</a>　 <a href="javascript:;">Q图系列[3]</a></dd>
			<dd>
				<?php require("includes/ubb.inc.php");?>
				<textarea name="content" rows="9"></textarea>
			</dd>
			
			<dd>
						验 证 码：
			<input type="text" name="code" class="text yzm"  /> <img src="code.php" id="code" onclick="javascript:this.src='code.php?tm='+Math.random();" /> 
						<input type="submit" class="submit" value="发表帖子" /></dd>
		</dl>
	</form>
	</div>
<?php 
require('includes/footer.inc.php');
?>